<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\manager\BaseManager;
use app\common\manager\FollowManager;
use think\Config;
use think\Exception;

/**
 * 队长中心
 */
class Follow extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 关注
     * @return void
     */
    public function follow()
    {
        $user = $this->user;
        $userId = $this->request->post('user_id');
        try {
            $result = FollowManager::follow($user,$userId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 取消关注
     * @return void
     */
    public function cancel()
    {
        $user = $this->user;
        $userId = $this->request->post('user_id');
        try {
            $result = FollowManager::cancel($user,$userId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }
}
